/*
 * Copyright (c) 2018 by TangChenBeiJian Information Technology Co.Ltd.
 *           All rights reserved
 */

package org.cx.redis;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * redis 服务
 *
 * @author Lin.Jianfei
 * @version 1.0.0
 * @since 2018-11-29
 */
@Service
public class RedisTemplateService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    public String getString(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }


    public void setString(String key, String jsonString, int timeout) {
        stringRedisTemplate.opsForValue().set(key, jsonString, timeout, SECONDS);
    }


    public boolean delString(String key) {
        return stringRedisTemplate.delete(key);
    }


    public <T> T getObject(String key) {
        return (T) redisTemplate.opsForValue().get(key);
    }

    public void setObject(String key, Object dto, int timeout) {
        redisTemplate.opsForValue().set(key, dto, timeout, TimeUnit.SECONDS);
    }

    public void setObject(String key, Object dto) {
        redisTemplate.opsForValue().set(key, dto, 30, TimeUnit.MINUTES);
    }

    public boolean updateExpireTime(String key, int timeout) {
        return stringRedisTemplate.expire(key, timeout, SECONDS);
    }


    public Long increment(String key) {
        return stringRedisTemplate.opsForValue().increment(key);
    }

}
